const mongoose = require('./db')
var Schema = mongoose.Schema

// brand(品牌表)
const brandSchema = new Schema({
    // 品牌名称
    brandName: String,
    // 品牌图片
    brandImg: String,
    // 品牌注册时间
    registrationTime: {
        type: Date,
        default: new Date,
    },
})

// category(商品分类表)
const categorySchema = new Schema({
    // 分类名称
    categoryName: String,
})

// coupon(优惠卷表)
const couponSchema = new Schema({
    // 优惠卷名称
    couponName: String,
    // 优惠卷价格高于minPrice字段才能使用
    minPrice: Number,
    // 优惠卷描述
    couponDescribe: String,
    // 可以优惠的价格 (商品35元 - preferentialPrice字段)
    preferentialPrice: String,
    // 优惠卷过期时间
    couponSchemaExpirationTime: Date
})

// variety(品种表)
const varietySchema = new Schema({
    varietyName: String
})

// commodity(商品表)
const commoditySchema = new Schema({
    // 商品名称
    commodityName: String,
    // 净重
    netWeight: Number,
    // 商品价格
    price: Number,
    // 商品详情图
    img:String,
    // 商品描述
    describe: String,
    // 过期时间
    expirationTime: Date,
    // 商品可以使用的优惠卷,数组类型
    couponList: Array,
    // 链接品牌表(brand)外键
    brand_id: {
        type: mongoose.Types.ObjectId,
        ref: "brand"
    },
    // 链接商家表(merchant)的外键
    business_id: {
        type: mongoose.Types.ObjectId,
        ref: "merchant"
    },
    // 链接品种表
    variety_id: {
        type: mongoose.Types.ObjectId,
        ref: "variety"
    },
    // 链接商品分类表(category)外键
    category_id: {
        type: mongoose.Types.ObjectId,
        ref: "category"
    },
})

// brand(品牌表)
const brandModel = mongoose.model('brand', brandSchema, 'brand')
// variety(品种表)
const varietyModel = mongoose.model('variety', varietySchema, 'variety')
// category(商品分类表)
const categoryModel = mongoose.model('category', categorySchema, 'category')
// category(商品表)
const commodityModel = mongoose.model('commodity', commoditySchema, 'commodity')

module.exports = { commodityModel,brandModel, categoryModel, commodityModel,varietyModel }